package com.auz.datagenerator.heuristics.value.gui;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.Range;
import org.jfree.data.function.Function2D;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import com.auz.datagenerator.heuristics.value.IValueHeuristic;

/**
 * Component for the value heuristics. The default component is specified as a 
 * unique panel made of a label and a slider configurable slider.
 * 
 * @author Aurelien Bonnel
 * @see JSlider
 * @see JLabel
 */
public class ValueHeuristicComponent extends JPanel implements ChangeListener {

	private static final long serialVersionUID = -5281142704106463022L;
	private IValueHeuristic parent;
	private JLabel label;
	private JSlider slider;
	private ChartPanel preview;
	private XYSeries trend;
	private XYSeriesCollection collection;
	
	/**
	 * Constructor
	 * @param _parent - the associated value heuristic
	 * @param _label - the label for this component
	 */
	public ValueHeuristicComponent(IValueHeuristic _parent, String _label) {
		this.parent = _parent;
		label = new JLabel(_label.concat(":"));
	}
	
	/**
	 * Sets the configuration parameters for the slider
	 * @param _min - the minimum value
	 * @param _max - the maximum value
	 * @param _start - the starting value
	 * @param _minSpacing - the spacing between minor ticks
	 * @param _majSpacing - the spacing between major ticks
	 */
	public void configureSlider(int _min, int _max, int _start, int _minSpacing, int _majSpacing) {
		slider = new JSlider();
		slider.setMinimum(_min);
		slider.setMaximum(_max);
		slider.setValue(_start);
		slider.setMajorTickSpacing(_majSpacing);
		slider.setMinorTickSpacing(_minSpacing);
		slider.setPaintLabels(true);
		slider.setPaintTicks(true);
		
		slider.addChangeListener(this);
	}
	
	/**
	 * Packs the label and prepares it for display. Only after calling this method can
	 * the panel be used.
	 */
	public void pack() {
		setLayout(new BorderLayout());
		
		add(label, BorderLayout.NORTH);
		add(slider, BorderLayout.CENTER);
		
		/* TODO: Representational chart creation and display
		preview = new ChartPanel(createChart());
		preview.setSize(slider.getSize());
		preview.setVisible(true);
		*/
		
		// add(preview, BorderLayout.SOUTH);
		
		setVisible(true);
	}

	

	/**
	 * Method called upon a change in the slider state
	 */
	public void stateChanged(ChangeEvent _e) {
		if(_e.getSource() == slider) {
			parent.changeParameter(slider.getValue());
		}
	}
	
	
	/*
	 * TODO: Implement the preview function panel
	 */
	/*
	private class PreviewFunction implements Function2D {

		public double getValue(double _x) {
			return (Double) parent.getRepresentationalValue(_x);
		}
		
	}
	
	private class ChartCreator {
		
		private JFreeChart createChart() {
			PreviewFunction f2 = new PreviewFunction();
			trend = DatasetUtilities.sampleFunction2DToSeries(f2, -5, 5, 20, "Trend");
			collection = new XYSeriesCollection(trend);
			JFreeChart chart = ChartFactory.createXYLineChart(
					"Trend and Periodicity", 
					"X",
					"X", 
					collection,
					PlotOrientation.VERTICAL,
					true,
					true,
					false);
			XYPlot plot = chart.getXYPlot();
			plot.getRenderer().setSeriesPaint(0, Color.BLACK);
			ValueAxis axis = new NumberAxis("Y");
			axis.centerRange(10);
			axis.setRange(new Range(-3, 3));
			plot.setRangeAxis(axis);
			return chart;
		}
	}
	*/
	
}	
